import numpy as np
import cv2

icol = (0, 138, 70, 255, 255, 255)


def open_camera():
    low_hue = icol[0]
    low_Sat = icol[1]
    low_Val = icol[2]

    high_Hue = icol[3]
    high_Sat = icol[4]
    high_Val = icol[5]

    cam = cv2.VideoCapture(0)
    while cam.isOpened():
        ok, frame = cam.read()
        if not ok:
            print('not open')
            break
        while (1):
            ret, frame = cam.read()
            # cv2.imshow('frame', frame)
            frame = cv2.GaussianBlur(frame, (7, 7), 0)
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            color_low = np.array([low_hue, low_Sat, low_Val])
            color_high = np.array([high_Hue, high_Sat, high_Val])
            mask = cv2.inRange(hsv, color_low, color_high)
            kernal = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
            mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal)
            mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)
            cv2.imshow('mask', mask)
            # reslut = cv2.bitwise_and(frame, frame, mask=mask)
            # circles = cv2.HoughCircles(reslut, cv2.HOUGH_GRADIENT, 1, 10, param2=30)
            # print(circles)
            # for x, y, r in circles[0]:
            #     cv2.circle(frame, (int(x), int(y)), int(r), thickness=1, lineType=cv2.LINE_AA)
            # cv2.imshow('Find_ROI', frame)
            # cv2.imshow('Find_ROI', reslut)

            k = cv2.waitKey(30)
            if k & 0xFF == ord('q'):
                break

        cv2.destroyAllWindows()


if __name__ == "__main__":
    open_camera()
